//? 本质是将多个函数组合到一起 依次触发

// 用法如下:
function fn1(x) {
    return x + 1;
}
function fn2(x) {
    return x + 2;
}
function fn3(x) {
    return x + 3;
}
function fn4(x) {
    return x + 4;
}
const a = compose(fn1, fn2, fn3, fn4);
console.log(a(1)); // 1+4+3+2+1=11
const b = compose(fn1);
console.log(b(1))
const c = compose();
console.log(c(1))
function compose(...fns) {
    if (fns.length === 0) return v => v
    else if (fns.length === 1) return (...args) => fns[0](...args)
    else {
        return fns.reduce((pre,cur) => (...args) => {
            return pre(cur(...args))
        })
    }
}
